//
//  ResultListViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/6.
//  Copyright © 2017年 史宵宵. All rights reserved.
//  成绩记录

#import "ResultListViewController.h"
#import "ResultInputViewController.h"
#import "GLLimitDatePickView.h"
#import "SendFriendCircleViewController.h"
#import "SendAdviceToParentsViewController.h"

@interface ResultListViewController ()
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation ResultListViewController
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //    [Global setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *titleStr;
    NSString *rightStr;
    if (self.monthListType == KResultMonthListType) {
        titleStr = @"成绩记录";
        rightStr = @"录入成绩";
    }else if (self.monthListType == KPersentMonthListType) {
        titleStr = @"家长寄语";
//        rightStr = @"写寄语";
    }
    
    self.title = titleStr;
    if (!strIsEmpty(rightStr)) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTitle:rightStr target:self selector:@selector(ResultInput)];
    }
     [self creatDataList];
}
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    NSString *url ;
    if (self.monthListType == KResultMonthListType) {
        url = [JSPathUtil urlAppHomePageGetHomeScoreList];
    }else if (self.monthListType == KPersentMonthListType) {
        url = [JSPathUtil urlAppInteractionGetParentSayList];
        if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {
            [dic setValue:self.childId forKey:@"parent_id"]; //家长的id
            [dic setValue:[JHUserInfo getUserInfo].userId forKey:@"child_id"]; //孩子的id
        }else{
            [dic setValue:self.childId forKey:@"child_id"]; //孩子的id
            [dic setValue:[JHUserInfo getUserInfo].userId forKey:@"parent_id"]; //家长的id
        }
    }
    [yjNetWorkManger requestType:POST andURL:url andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [self.dataArray removeAllObjects];

            NSArray *array = [NSArray arrayWithArray:response];
            [self.dataArray addObjectsFromArray:array];
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section][@"data"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (self.monthListType == KResultMonthListType) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@月", self.dataArray[indexPath.section][@"data"][indexPath.row][@"execute_month"]];
        cell.detailTextLabel.text= strIsEmpty(self.dataArray[indexPath.section][@"data"][indexPath.row][@"score_sum"])?@"":[NSString stringWithFormat:@"总分：%.1f分",[self.dataArray[indexPath.section][@"data"][indexPath.row][@"score_sum"] floatValue]];
    }else if (self.monthListType == KPersentMonthListType) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@月", self.dataArray[indexPath.section][@"data"][indexPath.row][@"mouth"]];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:self.dataArray[indexPath.section][@"data"][indexPath.row]];
    if (self.monthListType == KResultMonthListType) {
        ResultInputViewController *resultInputVC = [[ResultInputViewController alloc]init];
        resultInputVC.gadeIds = [NSString stringWithFormat:@"%@",dic[@"ids"]];
        resultInputVC.yearStr = self.dataArray[indexPath.section][@"execute_year"];
        resultInputVC.monthStr = dic[@"execute_month"];
        __weak typeof (self)unself = self;
        resultInputVC.resultInputSuccessBlock = ^(){
            [unself creatDataList];
        };
        [self.navigationController pushViewController:resultInputVC animated:YES];
    }else{
        SendAdviceToParentsViewController *sendAdviceToParentsVC = [[SendAdviceToParentsViewController alloc]init];
        sendAdviceToParentsVC.sendAdviceToParentsType = [[JHUserInfo getUserInfo].role_type isEqualToString:@"2"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"3"]||[[JHUserInfo getUserInfo].role_type isEqualToString:@"4"] ?KParentsSendAdviceToParentsType:KStudentSendAdviceToParentsType;
        sendAdviceToParentsVC.titleStr = [NSString stringWithFormat:@"%@年%@月",self.dataArray[indexPath.section][@"year"],dic[@"mouth"]];
        sendAdviceToParentsVC.sureContentStr = dic[@"context"];
        sendAdviceToParentsVC.scoreArray = dic[@"value"];
        __weak typeof (self)unself = self;
        sendAdviceToParentsVC.child_id = self.childId;
        sendAdviceToParentsVC.sendAdviceToParentsBlock = ^{
            [unself creatDataList];
        };
        [self.navigationController pushViewController:sendAdviceToParentsVC animated:YES];
        
//        SendFriendCircleViewController * sendFriendCircleVC = [[SendFriendCircleViewController alloc]init];
//        sendFriendCircleVC.sendType = KSendAdviceToParents;
//        sendFriendCircleVC.titleStr = [NSString stringWithFormat:@"%@年%@月",self.dataArray[indexPath.section][@"year"],dic[@"mouth"]];
//        sendFriendCircleVC.sureContentStr = dic[@"context"];
//        sendFriendCircleVC.score = [NSString stringWithFormat:@"%@",dic[@"value"]];
//        __weak typeof (self)unself = self;
//        sendFriendCircleVC.sendFriendCircleBlock = ^{
//            [unself creatDataList];
//        };
//        [self.navigationController pushViewController:sendFriendCircleVC animated:YES];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 44);
    view.backgroundColor = KBACKGROUNDCOLOR;
    
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(15, 12, 100, 20);
    label.font = [UIFont systemFontOfSize:18];
    if (self.monthListType == KResultMonthListType) {
        label.text = [NSString stringWithFormat:@"%@年",self.dataArray[section][@"execute_year"]];
    }else if (self.monthListType == KPersentMonthListType) {
        label.text = [NSString stringWithFormat:@"%@年",self.dataArray[section][@"year"]];
    }
    
    [view addSubview:label];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.01f;
}
#pragma mark - 成绩录入
- (void)ResultInput{
    GLLimitDatePickView *limitview=[[GLLimitDatePickView alloc]initDatePackerWithResponse:^(NSString *title) {
        MyLog(@"%@",title);
        if (self.monthListType == KResultMonthListType) {
            ResultInputViewController *resultInputVC = [[ResultInputViewController alloc]init];
            resultInputVC.resultInputType = @"newInput";
            resultInputVC.yearStr = [title substringToIndex:4];
            resultInputVC.monthStr = [title substringFromIndex:5];
            resultInputVC.resultInputSuccessBlock = ^(){
                [self creatDataList];
            };
            [self.navigationController pushViewController:resultInputVC animated:YES];
        }else{
            SendFriendCircleViewController * sendFriendCircleVC = [[SendFriendCircleViewController alloc]init];
            sendFriendCircleVC.sendType = KSendAdviceToParents;
            sendFriendCircleVC.courseId = self.childId;
            sendFriendCircleVC.titleStr = [NSString stringWithFormat:@"%@年%@月",[title substringToIndex:4],[title substringFromIndex:5]];
            sendFriendCircleVC.sendFriendCircleBlock = ^{
                [self creatDataList];
            };
            [self.navigationController pushViewController:sendFriendCircleVC animated:YES];
        }
    }];
    [limitview show];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
